from conan import ConanFile
from conan.tools.scm import Version
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import get, chdir, replace_in_file, copy, rmdir, export_conandata_patches, apply_conandata_patches
from conan.tools.microsoft import is_msvc, MSBuildToolchain, VCVars, unix_path
from conan.tools.layout import basic_layout
from conan.tools.gnu import Autotools, AutotoolsToolchain
from conan.tools.apple import is_apple_os
from conan.tools.build import cross_building
from conan.errors import ConanInvalidConfiguration
import os

class TremoloConan(ConanFile):
    name = "tremolo"
    license = "GPL"
    # url = "https://github.com/conan-io/conan-center-index"
    homepage = "http://wss.co.uk/pinknoise/tremolo/"
    description = "Tremolo is an ARM optimised version of the Tremor lib from xiph.org."
    topics = ("Ogg", "decompression")
    settings = "os", "arch", "compiler", "build_type"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
    }

    @property
    def _settings_build(self):
        return getattr(self, "settings_build", self.settings)
    
    def _is_mingw(self):
        return self.settings.os == "Windows" and self.settings.compiler == "gcc"
    
    def build_requirements(self):
        if self._settings_build.os == "Windows":
            if not self.conf.get("tools.microsoft.bash:path", check_type=str):
                self.tool_requires("msys2/cci.latest")
            self.win_bash = True
            
    def export_sources(self):
        export_conandata_patches(self)

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")
        self.settings.rm_safe("compiler.libcxx")
        self.settings.rm_safe("compiler.cppstd")

    def layout(self):
        basic_layout(self, src_folder=".")

    def source(self):
        get(self, **self.conan_data["sources"][self.version], destination=self.source_folder)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()

    def _patch_sources(self):
        apply_conandata_patches(self)
        replace_in_file(self, os.path.join(self.source_folder, "codec_internal.h"),
                        'floor1class  *class',
                        'floor1class  *klass')

        replace_in_file(self, os.path.join(self.source_folder, "floor1.c"),
                        'info->class',
                        'info->klass')

    def build(self):
        self._patch_sources()
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()

    def package_info(self):
        self.cpp_info.set_property("cmake_file_name", "tremolo")
        self.cpp_info.set_property("cmake_target_name", "tremolo::tremolo")
        self.cpp_info.set_property("pkg_config_name", "tremolo")

        self.cpp_info.libs = ["tremolo"]

        self.cpp_info.names["cmake_find_package"] = "tremolo"
        self.cpp_info.names["cmake_find_package_multi"] = "tremolo"
